<template>
  <div class="app-container">
    <p class="warn-content">
      Yaml编辑器 基于
      <a href="https://github.com/codemirror/CodeMirror" target="_blank">CodeMirror</a>，
      主题预览地址 <a href="https://codemirror.net/demo/theme.html#idea" target="_blank">Theme</a>
    </p>
    <Yaml :value="value" :height="height" />
  </div>
</template>

<script>
import Yaml from '@/components/YamlEdit/index'
export default {
  name: 'YamlEdit',
  components: { Yaml },
  data() {
    return {
      height: document.documentElement.clientHeight - 210 + 'px',
      value: '# 零知识证明是一种加密算法，其允许证明者向验证者证明拥有权限或信息，二无需实际泄露该消息，请在src/components/YamlEdit 目录中搜索原主题名称进行替换\n' +
        '换句话说，证明者可以向验证者证明其执行事件，而无需告知验证者是什么。这样确保在保护数据隐私同时，确保数据安全性和其他问题，这就是零知识证明(ZKPs)\n' +
        '\n' +
        '# ===================================================================\n' +
        '# Zero Knowledge Range Poofs,this is Main.\n' +
        '#\n' +
        '# This project is meant to show how to run a project using Hyperledger Ursa,\n' +
        '# Its supposed to be an example for how to ursa using rust.\n' +
        '#\n' +
        '# More information on profiles: https://gitee.com/gshhshh/zero_knowledge_range_proof\n' +
        '# More information on configuration properties: https://mp.weixin.qq.com/s/4XYDkusyuUrXXWbtVfdpUw\n' +
        '# ===================================================================\n' +
        '\n' +
        '# ===================================================================\n' +
        '# Zero Knowledge Range Poofs.\n' +
        '# Full reference is available at:\n' +
        '# https://gitee.com/gshhshh/zero_knowledge_range_proof\n' +
        '# ===================================================================\n' +
        '\n' +
        'async main() -> std::io::Result<()>{ \n' +
        ' println!("please input your id:");//一个提示输入\n' +
        'let mut id = String::new() //构造string型的id\n' +
        ' io::stdin().read_line(&mut id).expect("....");//以字符串进行输入\n' +
        'let bierthdate = get_birthate(id.trim());//进行身份认证,认证成功返回出生日期\n' +
        '//trim是取出id字符串信息.\n' +
        'let birth = birthdate.unwrap_or(0.to_string());//将取出的出生日期转换成字符串形式\n' +
        'println!("this is birth:{}",birth);\n' +
        '//进入零知识证明算法.\n' +
        'zero_knowledge_proof(&birth);\n' +
        '......\n' +
        '}\n' +
        '\n' +
        '# ===================================================================\n' +
        '# Zero Knowledge Range Poofs,this is get_birthate.\n' +
        '# Full reference is available at:\n' +
        '# https://gitee.com/gshhshh/zero_knowledge_range_proof\n' +
        '# ===================================================================\n' +
        '\n' +
        '///获取身份证出生日期函数\n' +
        'fn get_birthate(id_card:&str) ->Option<String> {\n' +
        '    //定义正则表达式匹配身份证号码\n' +
        '    let re = Regex::new(r"^\d{6}(\d{4})(\d{2})(\d{2})\d{3}[0-9Xx]$").unwrap();\n' +
        '    if let Some(caps) = re.captures(id_card) {\n' +
        '        //如果匹配成功，从捕获组中提取出生日期\n' +
        '        let year = caps.get(1).unwrap().as_str();//转换成&str类型\n' +
        '        let month = caps.get(2).unwrap().as_str();\n' +
        '        let day = caps.get(3).unwrap().as_str();\n' +
        '        //做一步规范日期判断\n' +
        '        if month >"12" || day > "31" {//月数是否符合规范\n' +
        '          return Some("is not legal day".to_string())\n' +
        '        }\n' +
        '        else if month == "02" && day > "29" {//2月天数是否符合规范\n' +
        '          return Some("is not legal day in month".to_string())\n' +
        '        }\n' +
        '        else if year == "0000" {\n' +
        '            return Some("is not legal input".to_string())\n' +
        '        }\n' +
        '        else{\n' +
        '          let birthdate = format!("{}",year);\n' +
        '          return Some(birthdate)\n' +
        '        }\n' +
        '    }//判断身份证长度是否符合规范\n' +
        '    else if id_card.len() < 18 {\n' +
        '      return Some("is not legal input".to_string())\n' +
        '    }\n' +
        '    else {\n' +
        '       None\n' +
        '    }\n' +
        '}\n' +
        '\n' +
        '# ===================================================================\n' +
        '# Zero Knowledge Range Poofs,this is zero_knowledge_proof.\n' +
        '# Full reference is available at:\n' +
        '# https://gitee.com/gshhshh/zero_knowledge_range_proof\n' +
        '# ===================================================================\n' +
        '\n' +
        '/// 进入零知识证明算法\n' +
        'fn zero_knowledge_proof(prov:&str) {\n' +
        '   //Example of using the Camenisch-Lysyanskaya signature and ZKPs\n' +
        '   let now = Utc::now();//获取当前系统时间\n' +
        '   let current_date = now.num_days_from_ce() / 365;\n' +
        '   //当前系统天数/365即为当前年份:类型i32\n' +
        '   let age = current_date - prov.parse::<i32>().unwrap();// &str changed i32\n' +
        '   println!("this is age:{}",age);\n' +
        '   let mut credential_schema_builder = Issuer::new_credential_schema_builder().unwrap();\n' +
        '   //创建age字段的样本模型\n' +
        '   credential_schema_builder.add_attr("age").unwrap();\n' +
        '   let credential_schema = credential_schema_builder.finalize().unwrap();\n' +
        '   //这种是对比跟credential_schema_builder\n' +
        '   let mut non_credential_schema_builder = NonCredentialSchemaBuilder::new().unwrap();\n' +
        '   let non_credential_schema = non_credential_schema_builder.finalize().unwrap();\n' +
        '   //根据创建好的样本获取公钥和私钥，公钥用于外发，私钥用于验证\n' +
        '   //Creating Issuer keys\n' +
        '   let (cred_pub_key, cred_priv_key, cred_key_correctness_proof) =\n' +
        '           Issuer::new_credential_def(&credential_schema, &non_credential_schema, true).unwrap();\n' +
        '   let master_secret = Prover::new_master_secret().unwrap();\n' +
        '   let credential_nonce = new_nonce().unwrap();\n' +
        '   let mut credential_values_builder = Issuer::new_credential_values_builder().unwrap();\n' +
        '   //将获取到经过计算的年龄添加进这个样本\n' +
        '   credential_values_builder.add_dec_known("age", &age.to_string()).unwrap();\n' +
        '   let cred_values = credential_values_builder.finalize().unwrap();\n' +
        '   //Blinding link secret\n' +
        '   //将公钥发给证明者\n' +
        '   let (blinded_credential_secrets,\n' +
        '        credential_secrets_blinding_factors,\n' +
        '        blinded_credential_secrets_correctness_proof,\n' +
        '       ) = Prover::blind_credential_secrets(\n' +
        '           &cred_pub_key,\n' +
        '           &cred_key_correctness_proof,\n' +
        '           &cred_values,\n' +
        '           &credential_nonce,\n' +
        '       ).unwrap();\n' +
        '   let cred_issuance_nonce = new_nonce().unwrap();\n' +
        '   //Signing the credential\n' +
        '   //获取登录证明进行验证\n' +
        '   let (mut cred_signature, signature_correctness_proof) = Issuer::sign_credential(\n' +
        '       "CnEDk9HrMnmiHXEV1WFgbVCRteYnPqsJwrTdcZaNhFVW",\n' +
        '       &blinded_credential_secrets,\n' +
        '       &blinded_credential_secrets_correctness_proof,\n' +
        '       &credential_nonce,\n' +
        '       &cred_issuance_nonce,\n' +
        '       &cred_values,\n' +
        '       &cred_pub_key,\n' +
        '       &cred_priv_key,\n' +
        '   ).unwrap();\n' +
        '   //Unblinding the signature\n' +
        '   //没有绑定进行公钥验证绑定\n' +
        '   Prover::process_credential_signature(\n' +
        '           &mut cred_signature,\n' +
        '           &cred_values,\n' +
        '           &signature_correctness_proof,\n' +
        '           &credential_secrets_blinding_factors,\n' +
        '           &cred_pub_key,\n' +
        '           &cred_issuance_nonce,\n' +
        '           None,\n' +
        '           None,\n' +
        '           None,\n' +
        '       ).unwrap();\n' +
        '   //判断这个样本的年龄age是否超过18周岁，超过创建证明\n' +
        '   //Create a proof that example_name is over 18\n' +
        '   let mut sub_proof_request_builder = Verifier::new_sub_proof_request_builder().unwrap();\n' +
        '       sub_proof_request_builder\n' +
        '           .add_predicate("age", "GE", 18)\n' +
        '           .unwrap();\n' +
        '   let sub_proof_request = sub_proof_request_builder.finalize().unwrap();\n' +
        '   let mut proof_builder = Prover::new_proof_builder().unwrap();\n' +
        '   proof_builder.add_common_attribute("master_secret").unwrap();\n' +
        '   proof_builder\n' +
        '       .add_sub_proof_request(\n' +
        '           &sub_proof_request,\n' +
        '           &credential_schema,\n' +
        '           &non_credential_schema,\n' +
        '           &cred_signature,\n' +
        '           &cred_values,\n' +
        '           &cred_pub_key,\n' +
        '           None,\n' +
        '           None,\n' +
        '       ).unwrap();\n' +
        '   //证明请求的一些初始化\n' +
        '   let proof_request_nonce = new_nonce().unwrap();\n' +
        '   let proof = proof_builder.finalize(&proof_request_nonce).unwrap();\n' +
        '   //构造证明验证机制，将创建的证明和公钥赋予它\n' +
        '   let mut proof_verifier = Verifier::new_proof_verifier().unwrap();\n' +
        '   proof_verifier\n' +
        '       .add_sub_proof_request(\n' +
        '           &sub_proof_request,\n' +
        '           &credential_schema,\n' +
        '           &non_credential_schema,\n' +
        '           &cred_pub_key,\n' +
        '           None,\n' +
        '           None,\n' +
        '       ).unwrap();\n' +
        '   //判断这个身份是不是合法的\n' +
        '   let is_valid = proof_verifier.verify(&proof, &proof_request_nonce).unwrap();\n' +
        '   println!("example_name >= 18: {}", is_valid);\n' +
        '   //Example of BLS Signature\n' +
        '   这里是几种登录认证的Demoing\n' +
        '   //Demoing BLS signature\n' +
        '   let generator = Generator::new().unwrap();\n' +
        '   let sign_key = SignKey::new(None).unwrap();\n' +
        '   let ver_key  = VerKey::new(&generator, &sign_key).unwrap();\n' +
        '   //获取一个会话消息\n' +
        '   let message = sha2::Sha256::digest(b"This is a test").to_vec();\n' +
        '   let signature = Bls::sign(message.as_slice(), &sign_key).unwrap();\n' +
        '   println!("valid_sig = {}", Bls::verify(&signature, message.as_slice(), &ver_key, &generator).unwrap());\n' +
        '   //Demoing Ed25519 signature\n' +
        '   let scheme = Ed25519Sha512::new();\n' +
        '   let (public, private) = scheme.keypair(None).unwrap();\n' +
        '   let signature = scheme.sign(message.as_slice(), &private).unwrap();\n' +
        '   let res = scheme.verify(message.as_slice(), signature.as_slice(), &public).unwrap();\n' +
        '   println!("verify = {}", res);\n' +
        '   //Demoing BitCoin Curve signature\n' +
        '   let scheme = EcdsaSecp256k1Sha256::new();\n' +
        '   let (public, private) = scheme.keypair(None).unwrap();\n' +
        '   let signature = scheme.sign(message.as_slice(), &private).unwrap();\n' +
        '   let res = scheme.verify(message.as_slice(), signature.as_slice(), &public).unwrap();\n' +
        '   println!("verify = {}", res);\n' +
        '   //Demoing Advanced BLS signatures\n' +
        '   let mut pks = Vec::new();\n' +
        '   let mut sigs = Vec::new();\n' +
        '   let generator = normal::Generator::generator();\n' +
        '   let msg = b"Hello World!";\n' +
        '   let ctx = b"";\n' +
        '   for _ in 0..10 {\n' +
        '       let (pk, sk) = normal::generate(&generator);\n' +
        '       let sig = normal::Signature::new(msg, Some(ctx), &sk);\n' +
        '       pks.push(pk);\n' +
        '       sigs.push(sig);\n' +
        '   }\n' +
        '   let mut asig = sigs[0].clone();\n' +
        '   asig.combine(&sigs[1..]);\n' +
        '   let mut apk = pks[0].clone();\n' +
        '   apk.combine(&pks[1..]);\n' +
        '   println!("Verity successful to zero_kownledge_range_proof!");\n' +
        '}\n' +
        '\n' +
        '# application:\n'
    }
  },
  mounted() {
    const that = this
    window.onresize = function temp() {
      that.height = document.documentElement.clientHeight - 210 + 'px'
    }
  }
}
</script>

<style scoped>

</style>
